package personresources.manager.dao.impl;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import personresources.manager.dao.EmployeeDao;
import personresources.manager.hibernate.Employee;
@Repository("employeeDao")
/**
 * Implement of EmployeeDao
 * @author Duy Chuong
 */
public class EmployeeDaoImpl extends GenericDaoImpl<Employee, Integer>
			implements EmployeeDao{
		
	/**
	 * Default contructor
	 */
	public EmployeeDaoImpl(){
		super(Employee.class);	
	}
	
	/*	
	 * @see personresources.manager.dao.EmployeeDao#getEmployees(java.lang.Integer)
	 */
	@SuppressWarnings("unchecked")
	public List<Employee> getEmployees(final Integer groupId){
		HibernateCallback callback = new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sqlCommand = new StringBuffer();
				sqlCommand.append("FROM Employee emp INNER JOIN FETCH emp.group");
				Query query;
				if (groupId != null && groupId.intValue() > 0) {
					sqlCommand.append(" WHERE emp.group.groupId=:groupId");
					query = session.createQuery(sqlCommand.toString());
					query.setInteger("groupId", groupId);
				} else {
					query = session.createQuery(sqlCommand.toString());
				}
				
				return query.list();
			}
		};
		return (List<Employee>)getHibernateTemplate().execute(callback); 
	}
	
	/**
	 * @see personresources.manager.dao.EmployeeDao#getEmployeeEmail(java.lang.String)
	 */
	public Employee getEmployeeEmail(String email) {		
		return (Employee) read("employeeEmail", email);
	}
	
}
